package com.ganzalang.gmall.aoplog.aspect;

import com.ganzalang.gmall.aoplog.annotation.SaveLog;
import com.ganzalang.gmall.aoplog.dto.OrderLogParamsDO;
import java.lang.annotation.Annotation;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @Description 保存日志切面
 * @Author Ganzalang
 * @Date 2021/4/24 16:20
 **/
@Aspect
@Component
@Slf4j
public class SaveLogAspect {


    @Pointcut("@annotation(com.ganzalang.gmall.aoplog.annotation.SaveLog)")
    private void pointCut() {

    }

    @AfterReturning(value = "pointCut() && @annotation(saveLog)" , returning = "result")
    public void handlerSaveLog(JoinPoint joinPoint, Object result, SaveLog saveLog) {
        Object[] args = joinPoint.getArgs();
        for (Object arg : args) {
            if (arg instanceof OrderLogParamsDO) {
                log.info("arg : {}", arg.toString());
                log.info("result : {}", result.toString());
                log.info("saveLog : {}", saveLog.type());
            }
        }
        log.info("This is aspect of @SaveLog");
    }
}
